<?php

/*
	[NianCode] (C)2010-2011 NianCode Inc.
	This is a freeware, use is subject not to license terms
	
	$Id: nc_db_mysql.inc.php 2010-4-17 19:29:26 NC nianks $
*/

if(!defined('IN_NC')) {
	exit('Access Denied');
}

class DBopt {
	
	var $query_nums = 0;
	var $db_name;
	var $link = null;
	var $query = '';
	var $select_sql = '';
	
	function __construct() {
		global $db_host, $db_user, $db_pw, $db_name, $db_charset;
		$this->connect($db_host, $db_user, $db_pw, $db_name);	
	}
	
	function connect($db_host, $db_user, $db_pw, $db_name = '', $pconnect = 0, $db_charset = 'gbk') {
		$connect = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
		$this->link = @$connect($db_host, $db_user, $db_pw) or die('ERROR: Database Connect Failed...');
		@mysql_select_db($db_name);
		$this->db_name = $db_name;
		@mysql_query('SET NAMES '.$db_charset); 	
	}
	
	function query($sql) {
		$query = mysql_query($sql);
		$this->query_nums++;
		return $query;	
	}
	
	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;	
	}
	
	function fetch_array($query, $type = MYSQL_ASSOC) {	
		$result = mysql_fetch_array($query, $type);
		return $result;
	}
	
	function affected_rows($query) {
		$affected = mysql_affected_rows($query);
		return $affected;
	}
	
	function insert_id() {
		return mysql_insert_id();
	}
	
	function fetch_first($sql) {
		return $this->fetch_array($this->query($sql));
	}
	
	function sqlinfo() {
		return 'sss';
	}
	
	function Get_select($sql) {
		$query = $this->query($sql);
		if($query) {
			while($res = $this->fetch_array($query)) {
				$result[] = $res;
			}
			return $result;
		} else {
			return FALSE;
		}
	}
	
	function Get_insert($sql) {
		$query = $this->query($sql);
		if($query) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	function Get_update($sql) {
		$query = $this->query($sql);
		if($query) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	function Get_delete($sql) {
		$query = $this->query($sql);
		if($query) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	function Get_nums($sql) {
		$query = $this->query($sql);
		if($query) {
			return $this->num_rows($query);
		} else {
			return FALSE;
		}
	}
	
	function close() {
		return mysql_close($this->link);
	}
}

?>